//
//  PushWebViewVC.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "PushWebViewVC.h"
#import "BlackManager.h"
#import "SetBlack.h"
#import "NewsDataBaseManager.h"
#import "LoginViewController.h"
#import "UMSocial.h"//友盟分享
@interface PushWebViewVC ()
@property (nonatomic, retain)UIButton *likeBtn;
@property (nonatomic, assign)NSInteger i;//判断是否存在数据库中
@property (nonatomic, assign)NSInteger mId;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UIWebView *webVC;
@property (nonatomic, assign)CGFloat size;
@end

@implementation PushWebViewVC
- (void)dealloc
{
    [_pathstr release];
    [_pathhtml release];
    [_model release];
    [_likeBtn release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(BackAction)] autorelease];
    //亮度按钮
    UIBarButtonItem *lightBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"light"] style:UIBarButtonItemStylePlain target:self action:@selector(lightAction:)];
    //分享shareBtn
    UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    //改变字体大小
    UIBarButtonItem *changeBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-22"] style:UIBarButtonItemStylePlain target:self action:@selector(jumpAction)];
    //收藏按钮
    self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _likeBtn.frame = CGRectMake(0, 0, 25, 25);
    //根据数据库中是否有该model设置收藏按钮样式
    self.arr = [[NewsDataBaseManager shareInstance] select];
    self.i = NO;
    for (AllDetailModel *m in _arr) {
        if ([_model.title isEqualToString:m.title]) {
            _i = YES;
        }
    }
    if (_i) {
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
    }else{
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
    }
    
    [_likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    //用按钮创建BarButton
    UIBarButtonItem *collectBar = [[UIBarButtonItem alloc]initWithCustomView:_likeBtn];
    self.navigationItem.rightBarButtonItems = @[collectBar, shareBtn, changeBtn, lightBtn];
    

    
    
    //webView
    self.webVC = [[UIWebView alloc]initWithFrame:CGRectMake(0, -62, self.view.frame.size.width, self.view.frame.size.height + 62)];
    _webVC.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_webVC];
    if ([BlackManager sharedInstance].isNight) {
        [_webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.background=‘#F6F7F3‘"];
    }
    //webView适应
    _webVC.scalesPageToFit = YES;
    if (_pathstr.length != 0) {
        [_webVC loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_pathstr]]];
    }else{
        
        [_webVC loadHTMLString:_pathhtml baseURL:[NSURL URLWithString:_pathhtml]];
    }
    [_webVC release];
    //判断webview亮度
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"webLight"];
    //给定字体大小
    self.size = 100;
}
#pragma mark - 调整亮度
-(void)lightAction:(UIButton *)btn
{
    BOOL dark = [[NSUserDefaults standardUserDefaults]boolForKey:@"webLight"];
    if (dark) {
        [_webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='gray'"];
        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"webLight"];
    }else{
         [_webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='white'"];
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"webLight"];

    }
}
#pragma mark - 设置字体大小的方法
-(void)jumpAction
{
    
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    //字体变大
    UIAlertAction *big = [UIAlertAction actionWithTitle:@"字体变大" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _size = _size + 20;
        NSString *str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%lf%%'", _size];
        //字体大小
        [_webVC stringByEvaluatingJavaScriptFromString:str];
    }];
    [alter addAction:big];
    //字体变小
    UIAlertAction *small = [UIAlertAction actionWithTitle:@"字体变小" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //
        _size = _size - 20;
        NSString *str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%lf%%'", _size];
        //字体大小
        [_webVC stringByEvaluatingJavaScriptFromString:str];
    }];
    [alter addAction:small];
    //取消
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        //
    }];
    [alter addAction:cancle];
    [self presentViewController:alter animated:YES completion:^{
        //
    }];
    
    
}
-(void)setNightDayModel
{
    [_webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='gray'"];
}
-(void)setLightDayModel
{
     [_webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='white'"];
}
#pragma mark - 分享方法
-(void)shareAction
{
    NSLog(@"分享了");
    NSString *picture =  [_model.media.firstObject objectForKey:@"url"];;
    [UMSocialData setAppKey:@"56a877ab67e58e508f000fb1"];
    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:picture];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:Nil shareText:_model.title shareImage:nil shareToSnsNames:@[UMShareToSina, UMShareToTencent,UMShareToRenren] delegate:nil];
}

#pragma mark - 收藏方法
-(void)likeAction:(UIButton *)btn
{
    if (_i) {
        //设置收藏属性
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
        _arr = [[NewsDataBaseManager shareInstance] select];
        for (AllDetailModel *m in _arr) {
            if ([m.title isEqualToString:_model.title]) {
                [[NewsDataBaseManager shareInstance] deleteNewsWithId:m.NewsId];
            }
        }
        
        NSLog(@"取消了收藏");
        _i = !_i;
        
    }else{
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
        [[NewsDataBaseManager shareInstance] insertNews:_model];
        [[NewsDataBaseManager shareInstance] select];
        _i = !_i;
    }
}

-(void)BackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
